package com.cn.ccp.action.system;

import java.io.File;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.FilePathBean;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;

/**
 * 用户个人信息修改
 * @author Administrator
 *
 */
public class UserAction extends BaseAction {
	private Logger log = Logger.getLogger(UserAction.class);
	private String userName ;
	private UserBean userBean ;
	
	public UserAction(){
		log.info("--------------UserAction()---------------");
		userBean = getUserService().findById(CCPContants.getUserBean().getId());
		if(userBean.getImage() != null && !"".equalsIgnoreCase(userBean.getImage().trim()) && !"images/teacher.png".equalsIgnoreCase(userBean.getImage().trim())){
			File sourseFile = null;
			if(userBean.getFile_path_id() !=  -1)
				sourseFile = new File(getFilePathService().findFilePathById(userBean.getFile_path_id()).getPath()+userBean.getImage());
			else
				sourseFile = new File(CCPContants.SYS_PATH+"files"+File.separator + userBean.getImage());
			
			File des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+CCPContants.getUserBean().getId() + File.separator);
			if(des.mkdir()){
				des.mkdirs();
			}
			des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+CCPContants.getUserBean().getId()  + File.separator +sourseFile.getName());
			CCPContants.copyFile(sourseFile.getPath(), des.getPath());
			userBean.setImage("tempfile/"+CCPContants.getCurrUserId()+"/"+des.getName());
		}
	}
	
	
	public void update(){
		if(userBean.getUserName().equals("")){
			showMsg("userName", "javax.faces.component.UIInput.REQUIRED");
			return;
		}
		if(userBean.getPassword().equals("")){
			showMsg("password", "javax.faces.component.UIInput.REQUIRED");
			return;
		}
		boolean passwordISChange = false ;
		if(userBean.getId() > 0 ){
			passwordISChange = getTeacherService().checkPasswrodIsChange(userBean.getId(), userBean.getPassword());
			if(passwordISChange){
				if(!userBean.getPassword().equals(userBean.getShowPassword())){
					showMsg("passwordok", "user_page_label_error_2");
					return ;
				}
			}
		}
		if(userBean.getDormitoryAddress().equals("")){
			showMsg("dormitoryAddress", "javax.faces.component.UIInput.REQUIRED");
			return;
		}
		if(userBean.getEmail().equals("")){
			showMsg("email", "javax.faces.component.UIInput.REQUIRED");
			return;
		}
		if(userBean.getMobile().equals("")){
			showMsg("mobile", "javax.faces.component.UIInput.REQUIRED");
			return;
		}
		if(userBean.getHomeAddress().equals("")){
			showMsg("homeAddress", "javax.faces.component.UIInput.REQUIRED");
			return;
		}
		if(userBean.getBirtdate().equals("")){
			showMsg("birtdate", "javax.faces.component.UIInput.REQUIRED");
			return;
		}
		if(userBean.getZipCode().equals("")){
			showMsg("zipCode", "javax.faces.component.UIInput.REQUIRED");
			return;
		}

		FilePathBean pathBean = getFilePathService().getFlagBean();
		String imgFile = pathBean.getPath()+userBean.getImage();
		File file = new File(imgFile);
		if(!file.exists()){
			UserBean tub = getUserService().findById(userBean.getId());
			userBean.setImage(tub.getImage());
		}else{
			try{
				userBean.setFile_path_id(pathBean.getId());
				String ext = userBean.getImage().split("\\.")[1];
				File des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+userBean.getId() + File.separator +"head-image."+ext);
				des.delete();
			}catch(Exception e){
				log.error("delete user["+userBean.getId()+"] old head image failed : ", e);
			}
//			String imagePath = CCPContants.getFilePath(userBean.getImage() , getFilePathService(),userBean.getFile_path_id() , userBean.getId());
//			File file1 = new File(imagePath);
//			
//			String headImagePath = file1.getParent()+File.separator + "head-image."+ext;
//			file1.renameTo(new File(headImagePath));
//			
//			String tmpImage = userBean.getImage();
//			userBean.setImage("tempfile/"+userBean.getId()+"/"+"head-image."+ext);
//			HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
//			session.setAttribute(Contants.USER_SESSION_BEAN ,userBean);
//			userBean.setImage(tmpImage);
		}
		
		boolean flag = getUserService().update(userBean,passwordISChange);
		if(flag)
			showMsg("showmsg", "user_page_label_self_info_update_success");
		else
			showMsg("showmsg", "user_page_label_self_info_update_error");
		if(userBean.getImage() != null && !"".equalsIgnoreCase(userBean.getImage().trim()) && !"images/teacher.png".equalsIgnoreCase(userBean.getImage().trim())){
			String filePaht = CCPContants.getFilePath(userBean.getImage() , getFilePathService(),userBean.getFile_path_id() ,userBean.getId() );
			File file2 = new File(filePaht);
			userBean.setImage("../../tempfile/"+CCPContants.getCurrUserId()+"/"+file2.getName());
		}
	}
	
	public void userImageClick(){
		
	}
	
	@SuppressWarnings("unchecked")
	public UserBean getUserBean() {
//		log.info("---------getUserBean() ----");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String fileName = varMap.get("fileName");
		String errorInfo = varMap.get("errorInfo");
		 
		//userBean = getUserService().findById(CCPContants.getUserBean().getId());
		if(fileName != null && !fileName.equals("")){
			userBean.setImage("files/"+fileName);
		}
		if(errorInfo != null && !errorInfo.equals("")){
			showMsg("image", errorInfo);
			userBean.setImage("");
		}
		if(userBean == null){
			userBean = new UserBean();
		} 
		if( userBean.getId() != 0 ){
			if(userBean.getUserType().equals("1")){
				userBean.setNameTitle("学生姓名");
				userBean.setAddressTitle("宿舍地址");
				userBean.setJoinTimeTitle("入学时间");
			}else{
				userBean.setNameTitle("教师姓名");
				userBean.setAddressTitle("办公室地址");
				userBean.setJoinTimeTitle("入职时间");
			}
		}
		
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}
 
}
